package com.example.deptempmanageservice.controller;

import com.myprojects.common.pojo.Employee;
import com.myprojects.common.pojo.EmployeeWithResume;
import com.myprojects.common.pojo.Nation;
import com.myprojects.common.pojo.Resume;
import com.example.deptempmanageservice.service.EmployeeService;
import com.example.deptempmanageservice.service.NationService;
import com.example.deptempmanageservice.service.ResumeService;
import com.myprojects.common.dto.Msg;
import com.myprojects.common.util.FormatJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/employee-curd")
public class ResumeController {
    //员工服务
    @Autowired
    EmployeeService employeeService;

    //简历服务
    @Autowired
    ResumeService resumeService;

    //民族代码查询服务
    @Autowired
    NationService nationService;


    /**
     * 该方法用于使用员工ID来获取简历
     */
    @RequestMapping(value = "/resume", method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmployeeWithResumeByEmployeeID(
            @RequestParam(value = "emp_id") Integer emp_id) {
        Msg result = null;
        try {
            Employee employee = employeeService.selectByPrimaryKey(emp_id);
            Resume resume = resumeService.selectResumeByEmployeeID(emp_id);

            result = Msg.succeed();
            result.addData("employee", employee);
            result.addData("resume", resume);
        } catch (Exception e) {
            e.printStackTrace();
            result = Msg.failed();
        }

        return result;
    }


    /**
     * 该方法用于保存员工信息和简历信息
     * 前端请求传Json对象则后端使用@RequestParam；
     * 前端请求传Json对象的字符串则后端使用@RequestBody。
     */
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmployeeAndResume(@Valid @RequestBody EmployeeWithResume employeeWithResume,
                                     BindingResult result) {
        Msg msg = null;
        Boolean success = false;//是否变更成功的标识
        System.out.println("传入的employeeWithResume为:");
        FormatJsonUtil.printJson(employeeWithResume.toString());

        //先检查用户信息有没有错误
        Map<String,Object> errorMap = resumeService.checkEmployeeAndResume(employeeWithResume,result);
        FormatJsonUtil.printJson("errorMap:"+errorMap);
        if (errorMap.size() > 0){
            success = false;
        }else{
            //检查完成后才能进行变更操作
            success = resumeService.saveEmployeeAndResume(employeeWithResume);
        }

        if (success){
            msg = Msg.succeed();
            msg.setMsg("保存员工简历成功！");
        }else {
            msg = Msg.failed();
            msg.setMsg("保存员工简历失败！");
            //将详细错误信息添加进msg
            msg.addData("fieldErrors",errorMap);
        }

        return msg;
    }


    /**
     * 该方法用于一次删除多个简历
     */
    @RequestMapping(value = "/resume", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteLotsEmployee(@RequestParam List<Integer> idList) {
        Msg msg = null;

        int iResult = employeeService.deleteLotsByPrimaryKey(idList);
        if (iResult > 0) {
            msg = Msg.succeed();
            msg.setMsg("删除成功，共删除" + iResult + "个员工");
        } else {
            msg = Msg.failed();
            msg.setMsg("删除失败!");
        }
        return msg;
    }


    /**
     * 该方法用于更新员工信息
     */
    @RequestMapping(value = "/resume", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmployee(@Valid @RequestBody Employee employee, BindingResult... result) {
        Msg msg = null;

        //先检查用户信息有没有错误
//        Map<String,Object> errorMap = employeeService.checkUpdateRole(employee,result);

        //如果都无误，则尝试保存
        if (true) {
            int iResult = 0;
            int iRelationResult = 0;
            try {
                iResult = employeeService.updateByPrimaryKeySelective(employee);
//                relationService.updateRelationByRole(role);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (iResult > 0) {
                msg = Msg.succeed();
                msg.setMsg("更新成功，共更新" + iResult + "个员工");
            } else {
                msg = Msg.failed();
                msg.setMsg("更新失败!");
            }
        } else {
            msg = Msg.failed();
            msg.setMsg("更新失败！");
            //将详细错误信息添加进msg
//            msg.addData("fieldErrors",errorMap);
        }

        return msg;
    }


    /**
     * 该方法用于获取民族代码
     */
    @RequestMapping(value = "/nation", method = RequestMethod.GET)
    @ResponseBody
    public Msg getNation() {
        Msg msg = null;

        List<Nation> nationList = nationService.getNation();
        if (nationList.size() > 0) {
            msg = Msg.succeed();
            msg.addData("nations", nationList);
        } else {
            msg = Msg.failed();
        }
        return msg;
    }

}
